package com.bootdo.program.wallpaper.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.config.Constant;
import com.bootdo.common.controller.BaseController;
import com.bootdo.program.wallpaper.domain.BzConfigDO;
import com.bootdo.program.wallpaper.service.BzConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.program.wallpaper.domain.BzQuotationsDO;
import com.bootdo.program.wallpaper.service.BzQuotationsService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 语录表-分享时随机取
 * 
 * @author guoyunfei
 * @email 2549789059@qq.com
 * @date 2021-08-12 21:24:13
 */
 
@Controller
@RequestMapping("/wallpaper/bzQuotations")
public class BzQuotationsController extends BaseController {
	@Autowired
	private BzQuotationsService bzQuotationsService;
	
	@GetMapping()
	@RequiresPermissions("wallpaper:bzQuotations:bzQuotations")
	String BzQuotations(){
	    return "wallpaper/bzQuotations/bzQuotations";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wallpaper:bzQuotations:bzQuotations")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//params.put("appid",queryAppid());
		//查询列表数据
        Query query = new Query(params);
		List<BzQuotationsDO> bzQuotationsList = bzQuotationsService.list(query);
		int total = bzQuotationsService.count(query);
		PageUtils pageUtils = new PageUtils(bzQuotationsList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wallpaper:bzQuotations:add")
	String add(){
	    return "wallpaper/bzQuotations/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wallpaper:bzQuotations:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		BzQuotationsDO bzQuotations = bzQuotationsService.get(id);
		model.addAttribute("bzQuotations", bzQuotations);
	    return "wallpaper/bzQuotations/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wallpaper:bzQuotations:add")
	public R save( BzQuotationsDO bzQuotations){
		String message = getMessage();
		if(null != message){
			return R.error(message);
		}
		Map<String, Object> params = new HashMap<>();
		//params.put("appid",getAppid());
		int count = bzQuotationsService.count(params);

		if(bzQuotations.getImg().indexOf("http") != -1){
			bzQuotations.setImg(null != bzQuotations.getImg() && !"".equals(bzQuotations.getImg()) ? bzQuotations.getImg() : null);
		}else{
			bzQuotations.setImg( getHttpUrl() + "/"+bzQuotations.getImg());
		}

		bzQuotations.setInde(count + 1);

		//bzQuotations.setAppid(getAppid());
		if(bzQuotationsService.save(bzQuotations)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wallpaper:bzQuotations:edit")
	public R update( BzQuotationsDO bzQuotations){
		String message = getMessage();
		if(null != message){
			return R.error(message);
		}

		if(bzQuotations.getImg().indexOf("http") != -1){
			bzQuotations.setImg(null != bzQuotations.getImg() && !"".equals(bzQuotations.getImg()) ? bzQuotations.getImg() : null);
		}else{
			bzQuotations.setImg( getHttpUrl() + "/"+bzQuotations.getImg());
		}

		//bzQuotations.setAppid(getAppid());
		bzQuotationsService.update(bzQuotations);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wallpaper:bzQuotations:remove")
	public R remove( Integer id){
		String message = getMessage();
		if(null != message){
			return R.error(message);
		}
		if(bzQuotationsService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wallpaper:bzQuotations:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		bzQuotationsService.batchRemove(ids);
		return R.ok();
	}


	
}
